﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using Honestysoft.BaseObject;
using System.Configuration;
using Honestysoft.Baseframework.Model;

namespace Honestysoft.Financing.WebUI.Module.Project
{
    /// <summary>
    /// ProjectInfo 的摘要说明
    /// </summary>
    public class ProjectInfo : IHttpHandler
    {
        private string GetPageUrl(Utils.ProjectTreeModule treeModule)
        {
            string pageUrl = "";
            switch (treeModule)
            {
                case Utils.ProjectTreeModule.ProjectCategory:
                    pageUrl = "BD_Project_List.aspx";
                    break;
                case Utils.ProjectTreeModule.Budget:
                    pageUrl = "BD_ProjectBudget_List.aspx";
                    break;
                case Utils.ProjectTreeModule.LoanAmount:
                    pageUrl = "BD_ProjectLoanAmount_List.aspx";
                    break;
                case Utils.ProjectTreeModule.GKLoanAmount:
                    pageUrl = "BD_ProjectGKLoanAmount_List.aspx";
                    break;
                case Utils.ProjectTreeModule.InvestmentPlan:
                    pageUrl = "BD_ProjectInvestmentPlan_List.aspx";
                    break;
                case Utils.ProjectTreeModule.Company:
                    pageUrl = "BD_ProjectCompany_List.aspx";
                    break;
                case Utils.ProjectTreeModule.AddInvestment:
                    pageUrl = "BD_ProjectAddInvestment_List.aspx";
                    break;
                case Utils.ProjectTreeModule.InitConfirm:
                    pageUrl = "ProjectCompleteList.aspx";
                    break;
                case Utils.ProjectTreeModule.Overview:
                    pageUrl = "SelectProjectInfo.html";
                    break;
                default:
                    break;
            }
            return pageUrl;
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string parentId = context.Request.Params["ID"];
            string functionId = context.Request.Params["FunctionID"];
            Utils.ProjectTreeModule treeModule = (Utils.ProjectTreeModule)int.Parse(context.Request.Params[Utils.ParamName_ProjectTreeModule]);
            string responseData = "";
            List<Honestysoft.DictionaryHelper.Sys_Dictionary> lsDict = Honestysoft.DictionaryHelper.DictionaryInstance.GetDictionaryList("ProjectCategory");
            string pageUrl = "";

            List<zTreeUtil.zTreeNode> lsZTreeNodes = new List<zTreeUtil.zTreeNode>();
            zTreeUtil.zTreeNode zNode = null;
            
            if (parentId == null) //根未加载
            {                
                zNode = new zTreeUtil.zTreeNode();
                zNode.icon = "../../images/computer.png";
                zNode.id = "0";
                zNode.isParent = (lsDict == null ? false : lsDict.Count > 0);
                zNode.name = "项目单位";
                zNode.nodeType = ((int)Utils.ProjectTreeNodeType.Root).ToString();
                zNode.target = "ifrmInfo";
                zNode.url = GetPageUrl(treeModule) + "?ID=0";
                lsZTreeNodes.Add(zNode);
            }
            else
            {
                //如果传入根节点
                if (parentId == "0")
                {
                    DataTable dt = null;
                    if (treeModule != Utils.ProjectTreeModule.ProjectCategory)
                    {
                        dt = new Honestysoft.Financing.BusinessLogic.Project("").GetProjectCategoryCount(null);
                    }
                    foreach (Honestysoft.DictionaryHelper.Sys_Dictionary item in lsDict)
                    {
                        zNode = new zTreeUtil.zTreeNode();
                        zNode.icon = "../../images/company.gif";
                        zNode.id = item.DictValue;
                        zNode.name = item.DictName;
                        zNode.nodeType = ((int)Utils.ProjectTreeNodeType.ProjectCategory).ToString();
                        zNode.target = "ifrmInfo";
                        zNode.url = GetPageUrl(treeModule) + "?ID=" + zNode.id;
                        if (treeModule == Utils.ProjectTreeModule.ProjectCategory)
                        {
                            zNode.isParent = false;
                        }
                        else
                        {
                            if (dt != null)
                            {
                                DataRow[] rows = dt.Select("ProjectCategory='" + zNode.id + "'");
                                if (rows != null && rows.Length == 1)
                                {
                                    zNode.isParent = Convert.ToInt32(rows[0]["ChildNodes"]) > 0;
                                }
                                else
                                {
                                    zNode.isParent = false;
                                }
                            }
                            else
                            {
                                zNode.isParent = false;
                            }
                        }
                        lsZTreeNodes.Add(zNode);
                    }
                }
                else//传入的是分类节点
                {
                    DataTable dtProject = new Honestysoft.Financing.BusinessLogic.Project("").GetProjectsByCategory(parentId,null);
                    foreach (DataRow dr in dtProject.Rows)
                    {
                        zNode = new zTreeUtil.zTreeNode();
                        zNode.icon = "../../images/database_gear.png";
                        zNode.id = dr["ID"].ToString();
                        zNode.isParent = false;
                        zNode.name = dr["SN"].ToString() + " " + dr["Name"].ToString();
                        zNode.nodeType = ((int)Utils.ProjectTreeNodeType.Project).ToString();
                        zNode.target = "ifrmInfo";
                        if (treeModule == Utils.ProjectTreeModule.InitConfirm)
                        {
                            pageUrl = "ProjectCompleteInfo.aspx?ID=" + zNode.id;
                        }
                        else if (treeModule == Utils.ProjectTreeModule.Overview)
                        {
                            pageUrl = "ProjectOverview.aspx?ID=" + zNode.id;
                        }
                        else
                        {
                            pageUrl = GetPageUrl(treeModule) + "?ID=" + zNode.id;
                        }
                        zNode.url = pageUrl;
                        lsZTreeNodes.Add(zNode);
                    }
                }
            }
            responseData = zTreeUtil.GetJsonString(lsZTreeNodes);
            context.Response.Write(responseData);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}